#include "player.h"
#include "../config.h"
#include "map.h"
#include "input.h"

Player::Player()
: Character(Map::CX_TILE * 14, Map::CY_TILE * 7)
{

}

bool Player::LoadRes()
{
	if (!m_kImage.Load(IMAGE_DIR "char.png"))
		return false;
	m_kImage.SetFrameInfo(32, 48, 3);

	return true;
}

void Player::FreeRes()
{
	m_kImage.Free();
}

void Player::Render(int mapX, int mapY)
{
	Character::Render(m_kImage, mapX, mapY);
}

void Player::HandleInput(Input const& kInput, Map& kMap)
{
	int const iGridX = this->m_x / Map::CX_TILE;
	int const iGridY = this->m_y / Map::CY_TILE;
	Map::Tile& kTile = kMap.GetTile(iGridX, iGridY);

	this->m_dx = this->m_dy = 0;

	if (kInput.up > 0) {
		if (!kMap.IsBlocked(iGridX, iGridY - 1)) {
			this->m_dy = -s_iMoveSpeed;
			kTile.bBlocked = false;
			kMap.GetTile(iGridX, iGridY - 1).bBlocked = true;
		}
		this->m_eDirection = UP;
	} else if (kInput.down > 0)	{
		if (!kMap.IsBlocked(iGridX, iGridY + 1)) {
			this->m_dy = s_iMoveSpeed;
			kTile.bBlocked = false;
			kMap.GetTile(iGridX, iGridY + 1).bBlocked = true;
		}
		this->m_eDirection = DOWN;
	} else if (kInput.left > 0)	{
		if (!kMap.IsBlocked(iGridX - 1, iGridY)) {
			this->m_dx = -s_iMoveSpeed;
			kTile.bBlocked = false;
			kMap.GetTile(iGridX - 1, iGridY).bBlocked = true;
		}
		this->m_eDirection = LEFT;
	} else if (kInput.right > 0) {
		if (!kMap.IsBlocked(iGridX + 1, iGridY)) {
			this->m_dx = s_iMoveSpeed;
			kTile.bBlocked = false;
			kMap.GetTile(iGridX + 1, iGridY).bBlocked = true;
		}
		this->m_eDirection = RIGHT;
	}
}